clear;
clc;

cd('specify directory here');
load('EdEx_matlab');

global Y T1 T2 SM SR Draws M Xc or orm Gmax Gmin ll 
options=optimoptions('fminunc','Algorithm','quasi-newton','Display','iter','PlotFcn',{@optimplotx,@optimplotfval,@optimplotfirstorderopt});

rng(10010);
Drawso=normrnd(0,1,6063,500);
M=500;

SM=(SM-mean(SM))/std(SM);
SR=(SR-mean(SR))/std(SR);

Gmax = max(f1rgp9std);
Gmin = min(f1rgp9std);

Y=Yo; T1=T1o; T2=T2o; SM=SMo; SR=SRo; Draws=Drawso; Xc=Xco;

theta2=[0;0;1;1;1;1;0;1;1;0.6;0.6;0.5;0.5;1;1;0.2;0.2;1;0.5;0.5];
eps=1e-5;
no = 0.1;
while no>eps
    theta = theta2;
    [theta2, FVAL]=fminunc(@(param)LogLikelihood_alt_bias2(param),theta,options);
    no=norm(theta2-theta);
end

PARAM = theta2;
var = ERRORS_alt_bias2(PARAM);
se = sqrt(diag(var));
